/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.agentslab.Broker;

import aa.core.Actor;
import aa.core.ActorName;
import aa.core.CommunicationException;
import aa.core.Contract;
import aa.core.Platform;
import aa.tool.CyberOrgTuple;
import com.agentslab.constants.APIConstants;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mehadihasan
 */
public class ResourceBroker extends Actor {

    
    private final String regMethodname = "register";
    private final String resourceListMethodName = "updateResourceList";
    private final String regGDMMethodname = "register";
    private final String deregCDMMethodname = "deregister";
    private final String searchMethodName = "searchAll";
    private ActorName anCybDM = null;
    private ActorName anGameDM = null;
    private final String serverStartMethod = "startServer";
    private final String statusMethod = "showStatus";

    public String getDeregCDMMethodname() {
        return deregCDMMethodname;
    }

    
    public String getRegGDMMethodname() {
        return regGDMMethodname;
    }


    public String getSearchMethodName() {
        return searchMethodName;
    }

    

    
  
    /**
     *
     * @return
     */
    public String getRegMethodname() {
        return regMethodname;
    }

   
    public ResourceBroker() {
    }

    /**
     * 
     * @param cyberOrgTuple
     * @param proposedContract
     */
    public void registerToCDM(CyberOrgTuple cyberOrgTuple, ActorName sender, String startTime) {

        try{
            send(APIConstants.cdmActor, this.getRegMethodname(), cyberOrgTuple);
            send(sender, statusMethod, "Successfully registered to CyberOrg directory manager.", startTime);
        } catch(Exception e){
            send(sender, statusMethod, "Resource registration failed!!");
        }
    }

    public void getNewResource(Contract contract, String gameName, String returnMethod, ActorName sender, String startTime) {
        try {

            CyberOrgTuple[] tempList = (CyberOrgTuple[]) call(APIConstants.cdmActor, searchMethodName, contract);
            Object[] arg = new Object[1];
            arg[0] = tempList;
            if(returnMethod.compareTo("")==0)
                send(sender, resourceListMethodName, tempList, startTime);
            else
                send(sender, returnMethod, tempList, startTime);
//            boolean isNegotiated = false;
//            for (CyberOrgTuple cyberOrgTuple : tempList) {
//                System.err.println(cyberOrgTuple);
//                if(negotiate(cyberOrgTuple, contract, gameName))
//                {
//                    System.err.println("Successful negotiation!!");
//                    isNegotiated = true;
//                    break;
//                }
//            }
//            if(!isNegotiated)
//            {
//                System.err.println("Sorry no suitable resource found!");
//            }
        } catch (CommunicationException ex) {
            ex.printStackTrace();
        }
    }

     public void negotiate(CyberOrgTuple cyberOrgTuple, Contract contract, String returnMethod, ActorName sender, String startTime) {

        Contract finalContract = null;

        try {
            ActorName remoteFaci = new ActorName(cyberOrgTuple.getDestination(), cyberOrgTuple.getId());
            finalContract = (Contract) call(remoteFaci, "negotiate", this.getActorName(),contract);
            if (finalContract != null) {
                System.out.println("negotiation successful... final contract: " + finalContract);
                cyberOrgTuple.setContract(finalContract);
                registerToGDM(cyberOrgTuple);
                if (returnMethod.compareTo("")==0)
                    send(sender, serverStartMethod, cyberOrgTuple, startTime);
                else
                    send(sender, returnMethod);
                //removeFromCDM(cyberOrgTuple);
            } else {
                System.err.println("failed negotiation");
                send(sender, serverStartMethod, "Negotiation failed!");

            }
        } catch (CommunicationException ex) {
            ex.printStackTrace();
        }

    }
    public void registerToGDM(CyberOrgTuple cyberOrgTuple) {
        send(APIConstants.gdmActor, this.getRegGDMMethodname(), cyberOrgTuple);
    }

    public void removeFromCDM(CyberOrgTuple cyberOrgTuple) {
        System.out.println(cyberOrgTuple.toString());
        send(APIConstants.cdmActor, this.getDeregCDMMethodname(), cyberOrgTuple);
    }

}
